﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace LXService
{
    public class Log
    {

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="e"></param>
        /// <param name="memo"></param>
        public static void SaveException(Exception e, string memo)
        {
            FileStream stream = new FileStream(GetLogDirectory("LogDirectory") + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.Append, FileAccess.Write, FileShare.Delete | FileShare.ReadWrite);
            StreamWriter writer = new StreamWriter(stream);
            writer.WriteLine("================================================================");
            writer.WriteLine(string.Format("Memo:\t{0}", memo));
            writer.WriteLine(string.Format("DateTime:\t{0}", DateTime.Now.ToString("HH:mm:ss.fff")));//DateTime.Now.ToShortTimeString()));
            writer.WriteLine(string.Format("Message:\t{0}", e.Message));
            writer.WriteLine(string.Format("StackTrace:\r\n----------\r\n{0}\r\n----------\r\n\r\n\r\n", e.StackTrace));
            stream.Flush();
            writer.Close();
            stream.Close();
            stream.Dispose();
            writer.Dispose();
        }

        /// <summary>
        /// 记录文本内容
        /// </summary>
        /// <param name="note"></param>
        public static void SaveNote(string note)
        {
            try
            {
                FileStream stream = new FileStream(GetLogDirectory("LogDirectory") + DateTime.Now.ToString("yyyy-MM-dd") + ".txt", FileMode.Append, FileAccess.Write, FileShare.Delete | FileShare.ReadWrite);
                StreamWriter writer = new StreamWriter(stream);
                writer.WriteLine("================================================================");
                writer.WriteLine(string.Format("Note:\t{0}", note));
                writer.WriteLine(string.Format("DateTime:\t{0}\r\n\r\n", DateTime.Now.ToString("HH:mm:ss.fff")));//DateTime.Now.ToShortTimeString()));
                stream.Flush();
                writer.Close();
                stream.Close();
                stream.Dispose();
                writer.Dispose();
            }
            catch { }
        }
        /// <summary>
        /// 获得Log目录
        /// </summary>
        /// <param name="category"></param>
        /// <returns></returns>
        public static string GetLogDirectory(string category)
        {
            string baseDirectory;
            baseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            if ((baseDirectory[baseDirectory.Length - 1] != '/') && (baseDirectory[baseDirectory.Length - 1] != '\\'))
            {
                baseDirectory = baseDirectory + @"\";
            }
            baseDirectory = string.Format(@"{0}Log\{1}\{2}\", baseDirectory, category, DateTime.Now.ToString("yyyyMM"));
            if (!Directory.Exists(baseDirectory))
            {
                Directory.CreateDirectory(baseDirectory);
            }
            return baseDirectory;
        }









    }
}
